<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderSa extends Model
{
    use HasFactory;

    public static function generateOrderNumber(): string
    {
        return date('Ymd') . str_pad(rand(0, 999999), 6, '0', STR_PAD_LEFT);
    }

    /**
     * 生成订单号
     */
    public static function createOrder(): string
    {
        do {
            // 生成一个新的订单号
            $newOrderNumber = self::generateOrderNumber();
            // 检查是否有重复的订单号
            $exists = OrderSa::where('order_number', $newOrderNumber)->exists();
        } while ($exists); // 如果订单号已经存在，继续生成

        return $newOrderNumber;
    }
}
